package com.example.zlx.controller;

import com.example.zlx.entity.User;
import com.example.zlx.service.UserService;
import com.example.zlx.util.Const;
import com.example.zlx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.xml.ws.ResponseWrapper;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


//    @GetMapping("/register1")
//    public String register1(){
//        return "adminregister";
//    }

    @PostMapping("/login")
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User>response=userService.login(username, password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return ServerResponse.createBySuccessMessage("登录成功");
//        return "index";
    }

    @PostMapping("/register")
    public ServerResponse<String>register(User user){
        return userService.register(user);
    }
}
